﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.TypeConvert;
using System;

namespace CatUtils.Utils.Enhance.TypeConvert
{
    /// <summary>
    /// Base64转码帮助
    /// </summary>
    public class Base64Convert
    {
        /// <summary>
        /// 转码为Base64
        /// </summary>
        /// <param name="Text"></param>
        /// <returns></returns>
        public string EnBase64(string Text)
        {
            byte[] ByteTemp = new ByteConvert().StrTobyte(Text);

            //转成 Base64 形式的 System.string
            Text = Convert.ToBase64String(ByteTemp);
            return Text;
        }

        /// <summary>
        /// 从Base64解码
        /// </summary>
        /// <param name="Base64Text"></param>
        /// <returns></returns>
        public string DeBase64(string Base64Text)
        {
            byte[] ByteTemp = Convert.FromBase64String(Base64Text);

            //转成 Base64 形式的 System.string
            Base64Text = new ByteConvert().ByteToStr(ByteTemp);
            return Base64Text;
        }

        /// <summary>
        /// 文件转换为Base64编码
        /// </summary>
        /// <param name="FilePath"></param>
        /// <returns></returns>
        public string FileToBase64(string FilePath)
        {
            string Base64Body = "";
            try
            {
                Base64Body = Convert.ToBase64String(new ByteConvert().FileToBytes(FilePath));
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }

            return Base64Body;
        }

        /// <summary>
        /// 文件转为可在网页端Src标签使用的文本
        /// </summary>
        /// <param name="FilePath"></param>
        /// <returns></returns>
        public string ImgToWebBase64(string FilePath)
        {
            return "data:image/jpeg;base64," + FileToBase64(FilePath);
        }
    }
}